'''
Module to merge two files data to one merged file
Created on Jul 12, 2014
@author: ANAND
'''
import os
import re

def merge(cur_name, pre_name, dest):
    
    in_path = dest + "/"
    cur_file = in_path + cur_name
    pre_file = in_path + pre_name
    
    out_path = dest + "/"
    
    mrg_name = "mrg_eb.dat"
    mrg_file = out_path + mrg_name
    
    data_dict = {}
    # Importing values from previous day file
    pre_lines = open(pre_file, "r").readlines()
    del pre_lines[0]
    pre_syms = []
    for pline in pre_lines:
        spline = re.split(',', pline)
        del spline[-1]
        # SYMBOL,SERIES,OPEN,HIGH,LOW,CLOSE,LAST,PREVCLOSE,TOTTRDQTY,TOTTRDVAL,TIMESTAMP,TOTALTRADES,ISIN
        sym_ser = spline[0] + "-" + spline[1]
        data_dict[sym_ser] = {"pre_symbol":spline[0],
                                "pre_series":spline[1],
                                "pre_open":spline[2],
                                "pre_high":spline[3],
                                "pre_low":spline[4],
                                "pre_close":spline[5],
                                "pre_ltp":spline[6],
                                "pre_volume":spline[8]
                               }
        pre_syms.append(sym_ser)
    
    # Importing values from current day file
    cur_lines = open(cur_file, "r").readlines()
    del cur_lines[0]
    cur_syms = []
    for cline in cur_lines:
        scline = re.split(',', cline)
        del scline[-1]
        sym_ser = scline[0] + "-" + scline[1]
        if sym_ser in data_dict.keys():
            data_dict[sym_ser].update({"cur_open":scline[2],
                                         "cur_high":scline[3],
                                         "cur_low":scline[4],
                                         "cur_close":scline[5],
                                         "cur_ltp":scline[6],
                                         "cur_volume":scline[8]
                                         })
        cur_syms.append(sym_ser)
    
    for psym in pre_syms:
        if psym not in cur_syms:
            del data_dict[psym]
    
    # Writing merged file of data from two days files
    out_file = open(mrg_file, "w")
    for symbol in data_dict:
        codata = data_dict[symbol]
        
        pre_pvt = (float(codata["pre_high"]) + float(codata["pre_low"]) + float(codata["pre_close"]))/3
        cur_pvt = (float(codata["cur_high"]) + float(codata["cur_low"]) + float(codata["cur_close"]))/3
        
        sym_id = codata["pre_symbol"] + "-" + codata["pre_series"]
        
        data_list = [sym_id, codata["pre_symbol"], codata["pre_series"], 
                     codata["cur_open"], codata["cur_high"], codata["cur_low"], codata["cur_close"], str(round(cur_pvt,2)), codata["cur_volume"],
                     codata["pre_open"], codata["pre_high"], codata["pre_low"], codata["pre_close"], str(round(pre_pvt,2))
                    ]
        
        # Write file
        data_line = "\t".join(data_list)
        data_line += "\n"
        out_file.writelines(data_line)
    
    out_file.close()
    os.remove(cur_file)
    os.remove(pre_file)
    return(mrg_name)
